package com.fangfang.home.adapter;

import android.content.Context;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.common.ui.circleimageview.CircleImageView;
import com.common.utils.DateTimeUtils;
import com.common.utils.GlideUtils;
import com.facebook.drawee.view.SimpleDraweeView;
import com.fangfang.R;
import com.fangfang.bean.home.FollowCardBean;
import com.fangfang.bean.home.ItemListBean;
import com.fangfang.bean.home.PictureFollowCardBean;
import com.fangfang.bean.home.TextCardBean;
import com.fangfang.home.views.FontTextView;

import java.util.ArrayList;
import java.util.List;

public class DailyPaperAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{

    public int TYPE_ONR = 1;
    public int TYPE_TWO = 2;
    public int TYPE_THREE = 3;
    public static final int TYPE_OTHER = -1;
    private LayoutInflater layoutInflater;
    private List<ItemListBean> infos = new ArrayList<>();
    protected Context context;

    public DailyPaperAdapter(Context context, List<ItemListBean> infos){
        this.context = context;
        this.infos = infos;
        layoutInflater = LayoutInflater.from(context);
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
       if(viewType == TYPE_ONR){
           return new TypeOneViewHolder(layoutInflater.inflate(R.layout.video_item_top_one,parent,
                   false));
       }else if(viewType == TYPE_TWO){
           return new TypeTwoViewHolder(layoutInflater.inflate(R.layout.video_item_one,parent,
                   false));
       }else if(viewType == TYPE_THREE){
           return new TypeThreeViewHolder(layoutInflater.inflate(R.layout.image_item_one,parent,
                   false));
       } else {
           return null;
       }
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        int viewType = getItemViewType(position);
        if(viewType == TYPE_ONR){
            TextCardBean textCardBean = infos.get(position).getTextCardBean();
            TypeOneViewHolder typeOneViewHolder = (TypeOneViewHolder) holder;
            typeOneViewHolder.tv_top_title.setText(textCardBean.getText());
        }else if(viewType == TYPE_TWO){
            FollowCardBean followCardBean = infos.get(position).getFollowCardBean();
            TypeTwoViewHolder typeTwoViewHolder = (TypeTwoViewHolder) holder;
            typeTwoViewHolder.iv_head.setTag(R.id.list_item_head, position);
            typeTwoViewHolder.simpledraweeview.setTag(R.id.list_item_bg, position);
            if(followCardBean!=null){
                if(followCardBean.getHeader()!=null){
                    GlideUtils.getInstance().loadHeadImageView(context,
                            followCardBean.getHeader().getIcon(),
                            typeTwoViewHolder.iv_head);
                    typeTwoViewHolder.tv_name.setText(followCardBean.getHeader().getTitle());
                }
                if(followCardBean.getContent()!=null){
                    typeTwoViewHolder.tv_title.setText(followCardBean.getContent().getTitle());
                    typeTwoViewHolder.tv_time_duration.setText(DateTimeUtils.format(followCardBean.getContent().getDuration()));
                    typeTwoViewHolder.tv_type.setText("#" + followCardBean.getContent().getCategory());
                    if(followCardBean.getContent().getCover()!=null){
                        typeTwoViewHolder.simpledraweeview.setImageURI(Uri.parse(followCardBean.getContent().getCover().getFeed()));
                    }
                }
            }
        }else if(viewType == TYPE_THREE){
            PictureFollowCardBean pictureFollowCard = infos.get(position).getPictureFollowCard();
            TypeThreeViewHolder typeThreeViewHolder = (TypeThreeViewHolder) holder;
            typeThreeViewHolder.iv_head.setTag(R.id.list_item_head, position);
            typeThreeViewHolder.simpledraweeview.setTag(R.id.list_item_bg, position);
            if(pictureFollowCard.getContent()!=null){
                typeThreeViewHolder.simpledraweeview.setImageURI(Uri.parse(pictureFollowCard.getContent().getUrl()));
                typeThreeViewHolder.tv_title.setText(pictureFollowCard.getContent().getDescription());
            }else {
                typeThreeViewHolder.simpledraweeview.setImageResource(R.drawable.shape_video_bg);
                typeThreeViewHolder.tv_title.setText("");
            }
            if(pictureFollowCard.getHeader()!=null){
                GlideUtils.getInstance().loadHeadImageView(context,
                        pictureFollowCard.getHeader().getIcon(),
                        typeThreeViewHolder.iv_head);
            }
        }
    }

    @Override
    public int getItemCount() {
        return infos != null ? infos.size() : 0;
    }

    @Override
    public int getItemViewType(int position) {
        String type =  infos.get(position).getType();
        if(type.equals("textCard")){
            return TYPE_ONR;
        }else if(type.equals("pictureFollowCard")){
            return TYPE_THREE;
        }else{
            return TYPE_TWO;
        }
    }

    class TypeOneViewHolder extends RecyclerView.ViewHolder{

        private TextView tv_top_title;
        public TypeOneViewHolder(@NonNull View itemView) {
            super(itemView);
            tv_top_title = (TextView) itemView.findViewById(R.id.tv_top_title);
        }
    }

    class TypeTwoViewHolder extends RecyclerView.ViewHolder{

        private TextView tv_title;
        private FontTextView tv_time_duration;
        private TextView tv_name;
        private TextView tv_type;
        private SimpleDraweeView simpledraweeview;
        private CircleImageView iv_head;

        public TypeTwoViewHolder(@NonNull View itemView) {
            super(itemView);
            simpledraweeview = (SimpleDraweeView) itemView.findViewById(R.id.simpledraweeview);
            iv_head = (CircleImageView) itemView.findViewById(R.id.iv_head);
            tv_title = (TextView) itemView.findViewById(R.id.tv_title);
            tv_time_duration = (FontTextView) itemView.findViewById(R.id.tv_time_duration);
            tv_name = (TextView) itemView.findViewById(R.id.tv_name);
            tv_type = (TextView) itemView.findViewById(R.id.tv_type);
        }
    }

    class TypeThreeViewHolder extends RecyclerView.ViewHolder{

        private TextView tv_title;
        private SimpleDraweeView simpledraweeview;
        private CircleImageView iv_head;

        public TypeThreeViewHolder(@NonNull View itemView) {
            super(itemView);
            simpledraweeview = (SimpleDraweeView) itemView.findViewById(R.id.simpledraweeview_image);
            iv_head = (CircleImageView) itemView.findViewById(R.id.iv_head_image);
            tv_title = (TextView) itemView.findViewById(R.id.tv_title_image);
        }
    }

    public void setDataSource(List<ItemListBean> infos) {
        if(infos!=null && infos.size()>0){
            this.infos = infos;
        }else{
            if(this.infos!=null && this.infos.size()>0){
                this.infos.clear();
            }
            this.infos = infos;
        }
        notifyDataSetChanged();
    }
}
